{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "1D convnet_IMDB.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "kjZKlfUY5Urc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IXP9B1M25mmU",
        "colab_type": "text"
      },
      "source": [
        "Conv1D layer, which has an interface similar to\n",
        "Conv2D . It takes as input 3D tensors with shape (samples, time, features) and\n",
        "returns similarly shaped 3D tensors."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8TnFsHse5nA4",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 156
        },
        "outputId": "f3809899-311b-4169-a592-8b85295d705c"
      },
      "source": [
        "from tensorflow.keras.datasets import imdb\n",
        "from tensorflow.keras.preprocessing import sequence\n",
        "\n",
        "max_features = 10000\n",
        "max_len = 500\n",
        "print('Loading data...')\n",
        "\n",
        "(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)\n",
        "print(len(x_train), 'train sequences')\n",
        "print(len(x_test), 'test sequences')\n",
        "print('Pad sequences (samples x time)')\n",
        "\n",
        "x_train = sequence.pad_sequences(x_train, maxlen=max_len)\n",
        "x_test = sequence.pad_sequences(x_test, maxlen=max_len)\n",
        "print('x_train shape:', x_train.shape)\n",
        "print('x_test shape:', x_test.shape)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Loading data...\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb.npz\n",
            "17465344/17464789 [==============================] - 0s 0us/step\n",
            "25000 train sequences\n",
            "25000 test sequences\n",
            "Pad sequences (samples x time)\n",
            "x_train shape: (25000, 500)\n",
            "x_test shape: (25000, 500)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "P1fwXJ3X6XIk",
        "colab_type": "text"
      },
      "source": [
        "# Training and evaluating a simple 1D convnet on the IMDB data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oRUI7GFH5yrw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 364
        },
        "outputId": "6d7ce84f-22c6-4592-be8e-ec8638569f59"
      },
      "source": [
        "from tensorflow.keras.models import Sequential\n",
        "from tensorflow.keras import layers\n",
        "from tensorflow.keras.optimizers import RMSprop\n",
        "\n",
        "model = Sequential()\n",
        "model.add(layers.Embedding(max_features, 128, input_length=max_len))\n",
        "model.add(layers.Conv1D(32, 7, activation='relu'))\n",
        "model.add(layers.MaxPooling1D(5))\n",
        "model.add(layers.Conv1D(32, 7, activation='relu'))\n",
        "model.add(layers.GlobalMaxPooling1D())\n",
        "model.add(layers.Dense(1))\n",
        "model.summary()\n",
        "\n",
        "model.compile(optimizer=RMSprop(lr=1e-4),\n",
        "loss='binary_crossentropy',\n",
        "metrics=['acc'])"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Model: \"sequential_1\"\n",
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "embedding_1 (Embedding)      (None, 500, 128)          1280000   \n",
            "_________________________________________________________________\n",
            "conv1d_2 (Conv1D)            (None, 494, 32)           28704     \n",
            "_________________________________________________________________\n",
            "max_pooling1d_1 (MaxPooling1 (None, 98, 32)            0         \n",
            "_________________________________________________________________\n",
            "conv1d_3 (Conv1D)            (None, 92, 32)            7200      \n",
            "_________________________________________________________________\n",
            "global_max_pooling1d_1 (Glob (None, 32)                0         \n",
            "_________________________________________________________________\n",
            "dense_1 (Dense)              (None, 1)                 33        \n",
            "=================================================================\n",
            "Total params: 1,315,937\n",
            "Trainable params: 1,315,937\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PgOJHbTF6ltY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 364
        },
        "outputId": "9ab5ea88-704b-499c-8e6f-40487a6b3c2b"
      },
      "source": [
        "history = model.fit(x_train, y_train,\n",
        "                    epochs=10,\n",
        "                    batch_size=128,\n",
        "                    validation_split=0.2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch 1/10\n",
            "157/157 [==============================] - 9s 58ms/step - loss: 0.7344 - acc: 0.5222 - val_loss: 0.6867 - val_acc: 0.5374\n",
            "Epoch 2/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.6663 - acc: 0.6601 - val_loss: 0.6671 - val_acc: 0.5870\n",
            "Epoch 3/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.6202 - acc: 0.7419 - val_loss: 0.6014 - val_acc: 0.7504\n",
            "Epoch 4/10\n",
            "157/157 [==============================] - 9s 54ms/step - loss: 0.5182 - acc: 0.8061 - val_loss: 0.4761 - val_acc: 0.8114\n",
            "Epoch 5/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.3992 - acc: 0.8484 - val_loss: 0.4211 - val_acc: 0.8410\n",
            "Epoch 6/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.3338 - acc: 0.8728 - val_loss: 0.4171 - val_acc: 0.8484\n",
            "Epoch 7/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.3002 - acc: 0.8928 - val_loss: 0.4082 - val_acc: 0.8636\n",
            "Epoch 8/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.2676 - acc: 0.9055 - val_loss: 0.3945 - val_acc: 0.8658\n",
            "Epoch 9/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.2420 - acc: 0.9155 - val_loss: 0.4310 - val_acc: 0.8694\n",
            "Epoch 10/10\n",
            "157/157 [==============================] - 9s 55ms/step - loss: 0.2226 - acc: 0.9231 - val_loss: 0.4432 - val_acc: 0.8740\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "N9cD0XsD7gmI",
        "colab_type": "text"
      },
      "source": [
        "Validation accuracy is\n",
        "somewhat less than that of the LSTM , but runtime is faster on both CPU and GPU\n",
        "\n",
        "1D convnet can offer a fast,\n",
        "cheap alternative to a recurrent network."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3_kVt_286vko",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}